package com.loyal.mwm.libs.api

import android.text.TextUtils
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import com.loyal.mwm.data.ResponseData
import com.loyal.mwm.ext.toObj
import com.loyal.mwm.libs.api.login.LoginRepository

class LoginViewModel : BaseViewModel<LoginRepository>(LoginRepository()) {

    private val _responseCallback = MutableLiveData<ResponseData<String?>>()
    val callback: LiveData<ResponseData<String?>> = _responseCallback

    private fun loadToken(tokenCallback: (token: String?) -> Unit) {
        /*val updateToken = MemoryUtil.updateQueueToken()
        if (!updateToken) {
            val token = MemoryUtil.readQueueToken()
            tokenCallback.invoke(token)
            return
        }*/
        viewModelLaunch({ repository.register() }) {
            if (it.error) {
                _responseCallback.postValue(it)
            } else {
                val tokenJson = it.data
                val resultBean = tokenJson.toObj(String::class.java)

                val code = resultBean?.code
                val message = resultBean?.message
                val success = TextUtils.equals("1", code)

                if (!success) {
                    val newData = it.copy(success = false, message = message)
                    _responseCallback.postValue(newData)
                } else {
                    val tokenData = resultBean?.data
                    //MemoryUtil.writeToken(tokenData)
                    tokenCallback.invoke(tokenData)
                }
            }
        }
    }

    fun requestMenu() {
        /*  cancel()
          loadToken { token ->
              viewModelLaunch({ repository.requestMenu(token) }) {
                  if (it.error)
                      _responseCallback.postValue(it)
                  else {
                      val result = it.data
                      val resultBean = result.toArray(QueueBean::class.java)
                      val code = resultBean?.code
                      val message = resultBean?.message
                      val success = TextUtils.equals("1", code)
                      val list = resultBean?.data

                      val newData = it.copy(
                          success = success, message = message, data = GsonUtil.list2Json(list)
                      )
                      _responseCallback.postValue(newData)
                  }
              }
          }*/
    }

    fun register() {
        viewModelLaunch({ repository.register() }) {
            _responseCallback.postValue(it)
        }
    }

    fun login(beanJson: String?) {
        viewModelLaunch({ repository.login(beanJson) }) {
            if (it.error)
                _responseCallback.postValue(it)
            else {
                val result = it.data
                val resultBean = result.toObj(String::class.java)
                val code = resultBean?.code
                val message = resultBean?.message
                val success = TextUtils.equals("1", code)
                val obj = resultBean?.data

                val newData = it.copy(
                    success = success, message = message, data = obj
                )
                _responseCallback.postValue(newData)
            }
        }
    }
}